package com.desheng.Service;


import com.desheng.bean.User;
import com.desheng.bean.UserExample;
import com.desheng.dao.UserMapper;
import com.desheng.model.StatusCode;
import com.desheng.util.MybatisUtil;
import com.easyFramework.annotation.Service;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @author yezl
 * @date 2018/5/30 0:04
 * @decription this is :
 */
@Service
public class UserService{




    public List<User> login(User user) {
        SqlSession session = MybatisUtil.openSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);
        UserExample ue = new UserExample();
        UserExample.Criteria uec = ue.createCriteria();
        uec.andUseridEqualTo(user.getUserid());
        List<User> users = userMapper.selectByExample(ue);
        return users;
    }



    public StatusCode register(User user) {
        SqlSession session = MybatisUtil.openSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);

        user.setMoney(1000);
        UserExample ue = new UserExample();
        UserExample.Criteria uec = ue.createCriteria();
        if (user.getUserid() != null)
            uec.andUseridEqualTo(user.getUserid());
        else
            return StatusCode.NONEUERID;
        List<User> users = userMapper.selectByExample(ue);
        if (users.size() > 0) {
            return StatusCode.REPEATED;
        } else {
            userMapper.insert(user);
        }
        session.commit();
        session.close();
        return StatusCode.PASS;
    }

    public StatusCode updateUser(User user) {

        SqlSession session = MybatisUtil.openSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);

        UserExample ue = new UserExample();
        UserExample.Criteria uec = ue.createCriteria();
        if (user.getUserid() != null)
            uec.andUseridEqualTo(user.getUserid());
        else
            return StatusCode.NONEUERID;
        List<User> users = userMapper.selectByExample(ue);

        if (users.size() > 0) {
            for (User u : users) {
                user.setPassword(u.getPassword());
                user.setMoney(u.getMoney());
            }
            userMapper.updateByPrimaryKey(user);
            session.commit();
            session.close();
            return StatusCode.PASS;

        } else {
            return StatusCode.NONEUERID;
        }

    }

    public User getUser(long userid){
        SqlSession session = MybatisUtil.openSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);
        User user=userMapper.selectByPrimaryKey(userid);
        session.close();
        return user;
    }


    public User find(String username, String password) {
        SqlSession session = MybatisUtil.openSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);
        System.out.println("username=" + username + "password=" + password);
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        criteria.andPasswordEqualTo(password);
//        User user = userMapper.findByUsername(username, password);
        List<User> users = userMapper.selectByExample(userExample);
        session.close();
        if (users.isEmpty()) {
            return null;
        }
        return users.get(0);
    }
}
